"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Tools-gs_initdb
Case Name   : 执行初始化数据库命令gs_initdb：同时使用--locale和--no-locale参数
Create At   : 2023/11/21
Owner       : @zou_jialiang050
Description :
    1.执行gs_initdb命令，同时指定--locale和--no-locale参数
    2.查看初始化后生成的目录
Expect      :
    1.执行成功
    2.生成初始化目录
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.userNode = Node('PrimaryDbUser')
        self.dir_path = os.path.join(macro.DB_INSTANCE_PATH, 'datadir')
    
    def test_systools(self):
        text = '-step1:执行gs_initdb命令，指定--locale和--no-locale;expect:成功-'
        self.logger.info(text)
        excute_cmd1 = f'source {macro.DB_ENV_PATH};' \
            f'gs_initdb -D {self.dir_path} --nodename=single ' \
            f'--locale=zh_CN.gbk --no-locale'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertNotIn('ERROR', msg1.upper(), text + '执行失败')
        text = '-----查看初始化后生成的目录;expect:成功-----'
        self.logger.info(text)
        excute_cmd2 = f'ls {self.dir_path}'
        self.logger.info(excute_cmd2)
        msg2 = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg2)
        self.assertIn('postgresql.conf', msg2, text + '执行失败')
    
    def tearDown(self):
        text = '-----清理环境-----'
        self.logger.info(text)
        excute_cmd1 = f'rm -rf {self.dir_path};ls {self.dir_path}'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.assertIn('No such file or directory', msg1, text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
